<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit"
                 @click="addOrUpdateHandle()"
      >
        新增配置
      </el-button>
    </div>

    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;"
    >
      <el-table-column label="序号" align="center" width="60">
        <template slot-scope="scope">
          {{ (pageIndex - 1) * pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间"
      >
      </el-table-column>
      <el-table-column
        prop="tenantNameCN"
        header-align="center"
        align="center"
        label="平台企业名称"
      >
      </el-table-column>
      <el-table-column
        prop="tenantName"
        header-align="center"
        align="center"
        label="契约锁配置企业名称"
      >
      </el-table-column>
      <el-table-column
        prop="accessKey"
        header-align="center"
        align="center"
        label="契约锁密钥key"
      >
      </el-table-column>
      <el-table-column
        prop="accessSecret"
        header-align="center"
        align="center"
        label="契约锁密钥"
      >
      </el-table-column>
      <el-table-column
        prop="sealId"
        header-align="center"
        align="center"
        label="印章ID"
      >
      </el-table-column>
      <el-table-column
        prop="categoryId"
        header-align="center"
        align="center"
        label="业务分类ID"
      >
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status" type="success">{{ scope.row.status | SignatureStatusToCh }}</el-tag>
          <el-tag v-else type="danger">{{ scope.row.status | SignatureStatusToCh }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="delSignature(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="block">
      <pagination v-show="totalPage > 0" :total="totalPage" :start.sync="pageIndex" :size.sync="pageSize"
                  @pagination="getDataList"
      />
    </div>

    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>
<script>
import { GetSignatureList, SignatureStatusToCh, DelSignature } from '@/api/SystemManagerApi'
import AddOrUpdate from './components/SignatureEdit'
import Pagination from '@/components/Pagination';

export default {
  filters: {
    SignatureStatusToCh
  },
  components: {
    AddOrUpdate,
    Pagination
  },
  data() {
    return {
      dataForm: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  created() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      var params = {
        'page': this.pageIndex,
        'limit': this.pageSize,
        'key': this.dataForm.key
      }
      GetSignatureList(params)
        .then((data) => {
          if (data && data.code === 20000) {
            this.dataList = data.data.rows
            this.totalPage = parseInt(data.data.total)
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
    },
    // 新增 | 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    delSignature(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        DelSignature(id).then((data) => {
          if (data.code = 20000) {
            this.$message.success(data.msg)
            this.getDataList()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }

  }
}
</script>
